package com.example.demo.controller;

import com.example.demo.annotation.Api;
import com.example.demo.domain.PageResult;
import com.example.demo.domain.exam.ExamPaper;
import com.example.demo.domain.exam.QueryModel;
import com.example.demo.domain.exam.condition.ExamPaperCondition;
import com.example.demo.dto.QuestionNumDTO;
import com.example.demo.dto.ResponseDto;
import com.example.demo.dto.reqDTO.DeleteExamPaperReqDTO;
import com.example.demo.service.exam.ExamPaperService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/exam/paper")
public class ExamPaperController {

    private final ExamPaperService examPaperService;

    @Api
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public PageResult<ExamPaper> page(@RequestBody QueryModel<ExamPaperCondition> query) {
        return examPaperService.page(query);
    }

    @Api
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public void createExamPaper(@RequestBody List<ExamPaper> examPapers) {
        examPaperService.createExamPaper(examPapers);
    }

    @Api
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void updateExamPaper(@RequestBody ExamPaper examPaper) {
        examPaperService.updateExamPaper(examPaper);
    }

    @Api
    @RequestMapping(value = "/get/all", method = RequestMethod.POST)
    public List<ExamPaper> getAllExamPaperByCondition(@RequestBody QueryModel<ExamPaperCondition> model) {
        return examPaperService.getAllExamPaperByCondition(model);
    }

    @Api
    @RequestMapping(value = "/random", method = RequestMethod.POST)
    public ExamPaper getRandomPaper(@RequestBody QuestionNumDTO dto) {
        return examPaperService.randomGroupPaper(dto);
    }

    @Api
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseDto DeleteExamPaper(@RequestBody DeleteExamPaperReqDTO param) {
        int count = examPaperService.DeleteExamPaper(param.getPaperId());
        if(count == 0) {
            return new ResponseDto<>().buildFail("更新失败");
        }
        return new ResponseDto<>().buildSucceedHasData("禁用成功");
    }
}
